import axios, { AxiosRequestConfig } from "axios";
import { useInterceptors } from "./interceptor";
import { IHttpRequest } from "./types";
import qs from "qs";

const http = axios.create();

const config: AxiosRequestConfig = {};

/**
 * 替换URL路由参数
 * @param {*} url URL地址
 * @param {*} paramName 参数名
 * @param {*} replaceWith 参数值
 */
const replacRouteVal = function (
  url: string,
  paramName: string,
  replaceWith: any
) {
  let re = new RegExp("{" + paramName + "}", "gm");
  return url.replace(re, replaceWith);
};

/**
 * 通用网络请求调用二次封装处理实现
 * 基于 Promise 对象实现更简单的 request 使用方式，支持请求和响应拦截
 */

/**
 * HTTP请示封装类
 */
const api: IHttpRequest = {
  debug: false,
  /**
   *  配置信息
   */
  config(opts: AxiosRequestConfig) {
    Object.assign(config, opts);
  },
  interceptors: useInterceptors,
  /**
   * 获取完整的服务器地址
   * @param url 获取完整的URL地址
   * @param baseUrl: 基础URL地址名称,可选，当baseUrl值为空时默认使用网关地址作为baseUrl
   */
  getServerUrl(url, baseUrl) {
    let _baseUrl = baseUrl || "";
    if (url && url.indexOf("://") !== -1) {
      return url;
    } else {
      return _baseUrl + url;
    }
  },
  /**
   * 发送网络请求
   * @param options
   */
  request(options: AxiosRequestConfig) {
    let opts = Object.assign({}, config, options);
    if (!opts.method) {
      opts.method = "GET";
    }
    if (opts.method === "GET" || opts.method === "get") {
      opts.params = Object.assign({}, options.params, options.data);
    }
    return http.request(opts);
  },
  /**
   * 发送网络请求  POST from (application/x-www-form-urlencoded)
   * @param url
   * @param data
   * @param options
   */
  formRequest(url: string, data: any, options: any) {
    let opts: AxiosRequestConfig = Object.assign(
      {},
      config,
      {
        headers: {
          "Content-Type": "application/x-www-form-urlencoded",
        },
      },
      options
    );
    opts.url = url;
    if (!opts.method) {
      opts.method = "GET";
    }
    if (opts.method === "GET" || opts.method === "get") {
      opts.params = Object.assign({}, options.params, options.data);
    } else if (data) {
      if (!(data instanceof FormData) && typeof data != "string") {
        opts.data = qs.stringify(data);
      } else {
        opts.data = data;
      }
    }
    return api.request(opts);
  },
  /**
   * GET
   * @param url
   * @param data
   * @param options
   */
  get(url: string, data: any, options: any) {
    let opts: AxiosRequestConfig = Object.assign({}, options);
    opts.url = url;
    opts.params = data;
    opts.method = "GET";
    return api.request(opts);
  },
  /**
   * POST
   * @param url
   * @param data
   * @param options
   */

  post(url: string, data: any, options: any) {
    let opts: AxiosRequestConfig = Object.assign({}, options);
    opts.url = url;
    opts.data = data;
    opts.method = "POST";
    return api.request(opts);
  },
  /**
   * PUT
   * @param url
   * @param data
   * @param options
   */
  put(url: string, data: any, options: any) {
    let opts: AxiosRequestConfig = Object.assign({}, options);
    opts.url = url;
    opts.data = data;
    opts.method = "PUT";
    return api.request(opts);
  },
  /**
   * DELETE
   * @param url
   * @param data
   * @param options
   */
  delete(url: string, data: any, options: any) {
    let opts: AxiosRequestConfig = Object.assign({}, options);
    opts.url = url;
    opts.data = data;
    opts.method = "DELETE";
    return api.request(opts);
  },
  /**
   *  上传文件
   * @param url
   * @param data
   */
  upload(url: string, data: any) {
    return axios({
      method: "POST",
      url,
      data,
      headers: {
        "Content-Type": "multipart/form-data",
      },
      timeout: 0,
    });
  },
};
export { http };
export default api;
